class CreditCard < ActiveMerchant::Billing::CreditCard
  
  
  
  ######## ONLY FOR TESTING PURPOSE THIS MODUL.
  ######## FOR REAL THING USE CREDITCARDVS
  
  
  
  
  #logging !!!!
  # put custom validation
  
  # ? do we need to check it using real payment like authorize
  
  #include all Creditcard Validation Methods from Active merchant
  
  # methods of CC merchant library
  # validate :validate_card
   
 
 
 
 =begin
   validate_customer_payment_profile(options)

 Verifies an existing customer payment profile by generating a test transaction

 Returns a Response object that contains the result of the transaction in params[‘direct_response’]
 Options

     * :customer_profile_id — The Customer Profile ID of the customer to use in this transaction. (REQUIRED)
     * :customer_payment_profile_id — The Customer Payment Profile ID of the Customer Payment Profile to be verified. (REQUIRED)
     * :customer_address_id — The Customer Address ID of the Customer Shipping Address to be verified.
     * :validation_mode — :live or :test In Test Mode, only field validation is performed. In Live Mode, a transaction is generated and submitted to the processor with the amount of $0.01. If successful, the transaction is immediately voided. (REQUIRED)

 [Source]
 
 =end
  def validate_card
    unless self.valid?
      self.errors.full_messages.each do |message|
        # errors.add_to_base message
        puts "hhh" + message.to_s
        #log message
      end
    end
  end
  
  
  def get_customer_profile(options ={})
    # get_customer_profile
  end
  
  
  def credit_card_create
  end
  
  def self.credit_card_store(options = {})
    #use settings later  in order to identify where to store in our tables with encription or in  Authorize.net 
    
    customer_profile(options)
    unless @customer_profile.success?
      # errors.add_to_base @customer_profile.message
      raise StandardError, @customer_profile.message
    
    # rescue ActiveMerchant::Errors => e
      #log
      # return false, @customer_profile
    end
    
    acc_data = {:customer_profile_id => @customer_profile.authorization}

    puts @customer_profile.authorization.to_s + "@customer_profile.authorization"
    acc_data
    options.merge ({:customer_profile_id => @customer_profile.authorization})
    customer_payment_profile(options)
           unless @customer_payment_profile.success?
             # errors.add_to_base @customer_payment_profile.message
             # rescue ActiveMerchant::Errors => e
             raise StandardError, @customer_payment_profile.message
             
             #log
             # return false, @customer_payment_profile
           end
           acc_data = {:customer_payment_profile_id => @customer_payment_profile.authorization}
           
  end
  
  
  
  # private
  def self.customer_profile(options = {})
    @customer_profile = ACCOUNT_GATEWAY.create_customer_profile({:profile => {:email => options[:user_id]}}) # change !!! user_id   indentifiation will be by other param
  end
  
  def self.customer_payment_profile(options = {})
    # if @customer_profile.test?    #success
    puts options[:customer_profile_id].to_s + "options[:customer_profile_id]"
    @customer_payment_profile = ACCOUNT_GATEWAY.create_customer_payment_profile(  :customer_profile_id => options[:customer_profile_id], #@customer_profile.authorization,
                                                                                  :payment_profile => {
                                                                                    :payment => {:credit_card => options[:credit_card]},
                                                                                    :bill_to => {:zip => options[:zipcode]}
                                                                                  })
    # else
    #   errors.add("Customer profile wasn't created", customer_profile.message)
    # end
    # test
    #  message
    #  authorization
    #     
 
  end
  
end
